package server

import (
	"bufio"
	"context"
	"fmt"
	"github.com/wangbin/jiebago"
	"io"
	"os"
	"qq_service/find_qun_by_word/protobuf/pb"
	"strings"
)

var seg jiebago.Segmenter

var arrlist []string

//N=86907937 行
func init() {
	seg.LoadDictionary("dict.txt")
	arrlist = LoadfiletoMEM()
	fmt.Println("初始化完成")
}

//服务结构体
type QunByWord struct {
	pb.UnimplementedFindQunByWordServiceServer
}

//HelloRequest请求
//HelloResponse返回结果
func (h QunByWord) FindQunByWord(ctx context.Context, in *pb.QqRequest) (*pb.QqResponse, error) {
	resp := new(pb.QqResponse)
	//resp.QQpassword=fmt.Sprintf("%s", FindQQPasswordByQQ(in.QQnum)    ) //输入in.Name，resp.message输出
	//resp.Qun
	//in.Word

	//
	//
	//
	resp.Email = in.Email

	var allword string
	for i := 0; i < len(in.Word)-1; i++ {
		allword += in.Word[i] + ","
	}
	allword += in.Word[len(in.Word)-1]
	resp.Word = allword

	mysend := NewSendRabbitMQ("amqp://guest:guest@localhost:5672/", "FindQQbyQun_task_queue")
	mylist := GetQQQunByWord(in.Word, in.Email)
	for i := 0; i < len(mylist); i++ {
		tmplist := strings.Split(mylist[i], "\t")
		qun := strings.Replace(tmplist[1], `"`, ``, -1)
		resp.Qun = append(resp.Qun, qun) //Qun
		//发送消息MQ  qun+"$#$"+Email+"$#$"+"英语"
		mysend.SendData(qun + "$#$" + resp.Word + "$#$" + resp.Email)
	}
	mysend.Close()
	//resp.Qun=
	return resp, nil
}

func GetQQQunByWord(word []string, filename string) []string {
	//var input string
	//fmt.Scanln(&input)
	//fmt.Println("要搜索的是",input)
	//软件工程，计算机
	var allword string
	for i := 0; i < len(word); i++ {
		allword += word[i]
	}
	allword += filename

	filesavepath := "C:\\Users\\Tsinghua-yincheng\\Desktop\\day15\\tmp\\"
	filelastpath := filesavepath + allword + ".txt"
	_, err := os.Stat(filelastpath)
	resultstring := []string{} //结果

	if err == nil {
		//存在
		fmt.Println("文件存在")
		//resultstring=append(resultstring,)
		file, _ := os.Open(filelastpath) //打开文件
		br := bufio.NewReader(file)
		for {
			line, _, err := br.ReadLine()
			if err == io.EOF {
				break
			}
			fmt.Println("来自缓存", string(line))
			resultstring = append(resultstring, string(line))

		}
		file.Close()

	} else {
		//不存在
		fmt.Println("文件不存在")

		savefile, _ := os.Create(filelastpath)
		save := bufio.NewWriter(savefile)

		var arr []string
		for i := 0; i < len(word); i++ {
			//allword+=word[i]
			arrch := seg.CutForSearch(word[i], true)
			arr = append(arr, word[i])
			arr = append(arr, returnstr(arrch)...)
		}

		for i := 0; i < len(arrlist); i++ {

			for j := 0; j < len(arr); j++ {
				if strings.Contains(string(arrlist[i]), arr[j]) {
					fmt.Println("来自搜索", string(arrlist[i]))
					//写入
					fmt.Fprintln(save, string(arrlist[i]))
					resultstring = append(resultstring, string(arrlist[i]))
					break //软件
				}
			}

		}
		save.Flush() //写入
		savefile.Close()

	}
	return resultstring

}

func LoadfiletoMEM() []string {
	const N = 86907937
	var arrlist []string = make([]string, N, N)
	filepath := "Z:\\K\\洗币\\社会工程学\\newQQ\\Allqundata.txt"
	file, _ := os.Open(filepath) //打开文件
	br := bufio.NewReader(file)
	i := 0
	for {
		line, _, err := br.ReadLine()
		if err == io.EOF {
			break
		}
		arrlist[i] = string(line)
		i++
		if i%1000000 == 0 {
			fmt.Println(i)
		}
		if i >= 5000000 {
			break
		}
	}
	file.Close()
	fmt.Println(i)
	return arrlist

}
func returnstr(ch <-chan string) []string {
	var strarr []string
	for word := range ch {
		fmt.Println(word)
		strarr = append(strarr, word)
	}
	return strarr
}
